package api

import (
	"strconv"
	"encoding/json"
	"gitee.com/DengAnbang/Wefore/src/bean"
)

var UserUtilityTimeCollect = make(map[string]UserUtilityTime, 0)

type UserUtilityTime struct {
	Account         string                  `json:"account"`
	UtilityTime     map[string]*UtilityTime `json:"-"`
	UtilityTimeShow []*UtilityTime          `json:"utility_time"`
}
type UtilityTime struct {
	Exe          string `json:"exe"`
	TimeInterval int    `json:"time_interval"`
}

func UploadWorkTimeRealize(account, exe, timeInterval string) error {
	IntervalTime, err := strconv.Atoi(timeInterval)
	if err != nil {
		return err
	}
	if userUtilityTime, ok := UserUtilityTimeCollect[account]; ok {
		if utilityTime, ok := userUtilityTime.UtilityTime[exe]; ok {
			utilityTime.TimeInterval = utilityTime.TimeInterval + IntervalTime
		} else {
			userUtilityTime.UtilityTime[exe] = &UtilityTime{Exe: exe, TimeInterval: 1}
		}
	} else {
		times := make(map[string]*UtilityTime)
		times[exe] = &UtilityTime{Exe: exe, TimeInterval: 1}
		UserUtilityTimeCollect[account] = UserUtilityTime{Account: account, UtilityTime: times}
	}
	return bean.NewSucceedMessage("上传成功!")
}
func GetWorkTimeRealize() error {
	//把map转为数组转json
	var res = make([]UserUtilityTime, 0)
	for _, v := range UserUtilityTimeCollect {
		for _, v1 := range v.UtilityTime {
			v.UtilityTimeShow = append(v.UtilityTimeShow, v1)
		}
		res = append(res, v)
	}
	bytes, _ := json.Marshal(res)
	return bean.NewSucceedMessage(string(bytes))
}
